<?php

namespace App\Http\Controllers\OpenShop\Feishu;

use App\Enums\BaseErrorCode;
use App\Enums\OpenShop\InvestmentPromotion\OpenShopCacheEnum;
use App\Enums\OpenShop\InvestmentPromotion\SignOrderEnum;
use App\Enums\OpenShop\InvestmentPromotion\UserOrderEnum;
use App\Enums\OpenShop\MissionTemplateEnum;
use App\Events\OpenShop\UserContractCreatedEvent;
use App\Exceptions\Custom\ResponseHttpException;
use App\Http\Controllers\BaseController;
use App\Http\Validate\OpenShop\InvestmentPromotion\OrderValidate;
use App\Http\Validate\OpenShop\InvestmentPromotion\UserContractValidate;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderLogModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopUserContractModel;
use App\Service\OpenShop\Feishu\PermissionService;
use App\Service\OpenShop\Feishu\ShopManualService;
use App\Service\OpenShop\Feishu\UserExtContractService;
use App\Service\OpenShop\FeishuMini\ShopService;
use App\Service\OpenShop\OrderService;
use App\Service\OpenShop\User\RmsUserService;
use App\Service\OpenShop\UserContractService;
use App\Support\ChangeSupport;
use Exception;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use Illuminate\Validation\ValidationException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class SignOrderManualController extends BaseController
{
    /**
     *
     */
    public function refreshManual(Request $request): JsonResponse
    {
        $orderNo      = $request->get('orderNo', '');
        $shopCode     = $request->get('shopCode', '');
        $provinceText = $request->get('provinceText', '');
        $cityText     = $request->get('cityText', '');
        $areaText     = $request->get('areaText', '');

        $shopManualService = new ShopManualService($orderNo);
        $shopManualService->genQualificationImage($shopCode,
            $provinceText . '-' . $cityText . '-' . $areaText, date('Ymd'));

        $orderLog = new OpenShopSignOrderLogModel();

        $orderLog->orderId      = $shopManualService->signOrder->id;
        $orderLog->content      = OpenShopSignOrderLogModel::TYPES[OpenShopSignOrderLogModel::UPDATE_REFRESH_MANUAL];
        $orderLog->type         = OpenShopSignOrderLogModel::UPDATE_REFRESH_MANUAL;
        $orderLog->createdByUid = getRmsUserInfoByAuth()->id;
        $orderLog->operatorName = getRmsUserInfoByAuth()->username;
        $orderLog->updatedByUid = getRmsUserInfoByAuth()->id;


        $orderLog->changes = json_encode([
            [
                "theme"      => "重新生成授权书",
                "changeType" => ChangeSupport::CHANGE_TYPE_SINGLE_UPDATE,
                "data"       => [
                    [
                        "key"         => "text",
                        "beforeValue" => '',
                        "afterValue"  => '',
                        "beforeText"  => $shopManualService->signOrder->baseInfoRel->chooseShopAddressPrefix,
                        "afterText"   => $provinceText . '-' . $cityText . '-' . $areaText,
                        "type"        => ChangeSupport::FILED_TYPE_STRING,
                        "description" => "重新生成授权书"
                    ]
                ]
            ]
        ], JSON_UNESCAPED_UNICODE);

        $orderLog->save();

        return $this->success();
    }
}
